package org.hy.pay.message.web.interceptors;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.hy.pay.message.exception.MessageBizException;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;



/**
 * 鏉冮檺鎷︽埅鍣�
 * 
 * @author lzm
 * 
 */
public class AuthInterceptor implements HandlerInterceptor {
	 
	private static final Logger logger = Logger.getLogger(AuthInterceptor.class);
	private List<String> excludeUrls;
    
	//private List<String> loginView;
	
//	@Autowired
//	private TokenManagerService manager;

	public List<String> getExcludeUrls() {
		return excludeUrls;
	}

	public void setExcludeUrls(List<String> excludeUrls) {
		this.excludeUrls = excludeUrls;
	}
	

	/**
	 * 鍦╟ontroller鍚庢嫤鎴�
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {

	}

	/**
	 * 鍦╟ontroller鍓嶆嫤鎴�
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws MessageBizException,Exception {
		String requestPath = request.getRequestURI() + "?" + request.getQueryString();
		requestPath = requestPath.substring(request.getContextPath().length() + 1);
		if(requestPath.contains("?")){
			requestPath = requestPath.substring(0,requestPath.indexOf("?"));
		}
		
		/*String authorization  = request.getHeader(Constants.AUTHORIZATION);
		
		Cookie  tokenCookie  = null;
		if(StringUtil.isEmpty(authorization)) {
			authorization = request.getParameter(Constants.AUTHORIZATION);
		}
		if(StringUtil.isEmpty(authorization)) {
			tokenCookie = CookieUtil.getCookieByName(request, Constants.AUTHORIZATION);
			if(tokenCookie != null)
			authorization = tokenCookie.getValue();
		}
		Integer userId  = manager.getUserId(authorization);*/
		
		
/*		
		for(String view: loginView){
			if(requestPath.endsWith(view))
			{
				if(userId != null){
					return true;
				}
				
				forward(request, response); //PC端登录
				return false;
				
			}
		}
			if(requestPath.endsWith("html")){
				return true;
			}*/
		
		
			for(String url : excludeUrls){
				if(requestPath.startsWith(url))
				{
					return false;
				}
			}
			
			/*if(authorization == null || "undefined".equals(authorization)){
				CookieUtil.delCookie(request, response, Constants.AUTHORIZATION);
			throw new CustomException(CommonConstant.USER_NOT_LOGIN, CommonConstant.USER_NOT_LOGIN_STR);
		    }
		
			if(userId==null){
				if(tokenCookie != null)
					CookieUtil.delCookie(request, response, Constants.AUTHORIZATION);
				throw new CustomException(CommonConstant.EXCEPTION_ERROR_TOKEN, CommonConstant.EXCEPTION_ERROR_TOKEN_STR);
			}
			*/
			
			return true;
		}
		

	

/*	private void forward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String address = null;
		System.out.println(request.getRequestURI());
		System.out.println(request.getContextPath().length());
	    String queryString = request.getQueryString();
	    System.out.println("queryString ="+queryString);
	    if(queryString != null ){
	    	 address =  (request.getRequestURI() + "?" + request.getQueryString()).substring(request.getContextPath().length() + 1);
	    }else{
	    	 address =  (request.getRequestURI()).substring(request.getContextPath().length() + 1);
	    }
	    
		String localUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		response.sendRedirect(localUrl+"xcfh/index.html");
		return;
	}*/

}
